package com.bankLogisticsSys.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.bankLogisticsSys.menu.MenuManager;
import com.bankLogisticsSys.util.WeixinUtil;
import com.jfinal.weixin.sdk.api.ApiConfig;
import com.jfinal.weixin.sdk.api.ApiResult;
import com.jfinal.weixin.sdk.api.MenuApi;
import com.jfinal.weixin.sdk.jfinal.ApiController;

//公众号菜单设置
public class MenuApiController extends ApiController {
	
	private static final Logger logger = LoggerFactory
			.getLogger(MenuApiController.class);

	/**
	 * 如果要支持多公众账号，只需要在此返回各个公众号对应的  ApiConfig 对象即可
	 * 可以通过在请求 url 中挂参数来动态从数据库中获取 ApiConfig 属性值
	 */
	public ApiConfig getApiConfig() {
		return WeixinUtil.getApiConfig();
	}

	/**
	 * 获取公众号菜单
	 */
	public void getMenu() {
		ApiResult apiResult = MenuApi.getMenu();
		if (apiResult.isSucceed())
			renderText(apiResult.getJson());
		else
			renderText(apiResult.getErrorMsg());
	}

	/**
	 * 创建菜单
	 */
	public void createMenu()
	{
		String str = MenuManager.getMainMenu();
		System.out.println(str);
		ApiResult apiResult = MenuApi.createMenu(str);
		if (apiResult.isSucceed())
			renderText(apiResult.getJson());
		else
			renderText(apiResult.getErrorMsg());
	}
	/**
	 * 创建个性化菜单
	 */
	public void addConditional()
	{
		int flag = getParaToInt(0, 1);
		String str = getMenuStr(flag);
		logger.info(str);
		ApiResult apiResult = MenuApi.addConditional(str);
		if (apiResult.isSucceed())
			renderText(apiResult.getJson());
		else
			renderText(apiResult.getErrorMsg());
	}
	/**
	 * 删除所有菜单
	 */
	public void deleteMenu(){
		ApiResult apiResult = MenuApi.deleteMenu();
		renderText(apiResult.getJson());
	}
	public String getMenuStr(int flag){
		String jsonMenu;
		switch(flag){
		case 1:jsonMenu = MenuManager.getBankSMenu();break;
		case 2:jsonMenu = MenuManager.getBankMMenu();break;
		case 3:jsonMenu = MenuManager.getNetworkMenu();break;
		case 4:jsonMenu = MenuManager.getCenterMenu();break;
		case 5:jsonMenu = MenuManager.getPostmanMenu();break;
		default:jsonMenu = "";
		}
		return jsonMenu;
	}
}
